<template>
  <div>
    用户列表
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="15">
        <el-col :span="10">
          <el-input placeholder="请输入查询内容" v-model="queryInfo.query">
            <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible=true">添加用户</el-button>
        </el-col>
      </el-row>
      <el-table :data="userlists" border style="width: 100%">
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="username" label="用户名"></el-table-column>
        <el-table-column prop="password" label="密码" width="150"></el-table-column>
        <el-table-column prop="email" label="邮箱" width="150"></el-table-column>
        <el-table-column prop="role" label="身份"></el-table-column>
        <el-table-column prop="state" label="状态" width="100">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.state" @change="updateState(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.id)"></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteUser(scope.row.id)"
            ></el-button>
            <el-tooltip effect="dark" content="分配权限" placement="top-start" :enterable="false">
              <!--文字提示 enterable 隐藏-->
              <el-button type="warning" icon="el-icon-setting" size="mini"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[1, 2, 5, 100]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
    <el-dialog title="添加用户" :visible.sync="addDialogVisible" width="50%" @close="handleClose">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="ruleForm.email"></el-input>
        </el-form-item>
        <!-- <el-form-item label="身份" prop="role">
          <el-input v-model="ruleForm.role"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-input v-model="ruleForm.state"></el-input>
        </el-form-item>-->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible=false">取消</el-button>
        <el-button type="primary" @click="addUser">确定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="修改用户" :visible.sync="updateDialogVisible" width="50%" @close="handleCloses">
      <el-form :model="ruleForms" :rules="ruless" ref="ruleForms" label-width="100px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForms.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForms.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="ruleForms.email"></el-input>
        </el-form-item>
      </el-form>
       <span slot="footer" class="dialog-footer">
        <el-button @click="updateDialogVisible=false">取消</el-button>
        <el-button type="primary" @click="updateUser">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      queryInfo: {
        query: "",
        pageNum: 1,
        pageSize: 5
      },
      userlists: [], // 用户列表
      total: 0 ,// 最大数据记录
      addDialogVisible:false,
      ruleForm: {
          username: '',
          password: '',
          email: '', 
        },
      rules:{
         username:[
           { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 5, max: 8, message: "长度在 5 到 8 个字符", trigger: "blur" }
        ],
        password:[
           { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 8, message: "长度在 6 到 8 个字符", trigger: "blur" }
        ],
        email:[
           { required: true, message: "请输入邮箱", trigger: "blur" },
          { min: 5, max: 15, message: "请输入正确邮箱地址", trigger: "blur" }
        ]
      },
      updateDialogVisible:false,
      ruleForms:{},
      ruless:{
        password:[
           { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 8, message: "长度在 6 到 8 个字符", trigger: "blur" }
        ],
        email:[
           { required: true, message: "请输入邮箱", trigger: "blur" },
          { min: 5, max: 15, message: "请输入正确邮箱地址", trigger: "blur" }
        ]
      },
      
   
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    // async getUserList() {
    //   const { data: res } = await this.$http.get("/users", {params: this.queryInfo});
    //   this.userLists = res.data;
    //   this.total = res.number;

    // }
    async getUserList() {
      // 调用post请求
      const { data: res } = await this.$http.get("/users", {
        params: this.queryInfo
      });
      this.userlists = res.listuser; // 将返回数据赋值
      this.total = res.number; // 总个数
      // console.log(res.number)
      // console.log(this.userlists);
    },
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getUserList();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getUserList();
    },
    async updateState(userinfo) {
      const { data: res } = await this.$http.put(
        `/states?id=${userinfo.id}&state=${userinfo.state}`
      );
      if (res != "success") {
        userinfo.state = !userinfo.state;
        return this.$message.error("操作失败！！！");
      }
      this.$message.success("操作成功！！！");
    },

    // async updateState(userInfo){
    //   var formData=new FormData()
    //   formData.append("id",userInfo.id);
    //   formData.append("state",userInfo.state);
    // const {data:res}=await  this.$http.put("/states",formData)
    // if(res!="success"){
    //   // userInfo.state=!userInfo.state;
    //   // return this.$message.error("操作失败")
    // }
    //    if(res=="success")
    //    this.$message.success("操作成功")    }
  handleClose(){
    this.$refs.ruleForm.resetFields();
  },

   addUser() {
        this.$refs.ruleForm.validate(async valid => {
        if(!valid)
        return ;
        const {data:res}=await this.$http.post("/addUser",this.ruleForm)
      //   if (res != "success") {
      // userinfo.state = !userinfo.state;
      // return this.$message.error("操作失败！！！");
      // }
      if(res=="success")
      this.$message.success("操作成功！！！");
      //隐藏
      this.addDialogVisible = false;
      this.getUserList();
        });
      },
    async deleteUser(id){
        const confirmResult = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if(confirmResult=='confirm'){
         const {data:res} = await this.$http.delete("/deleteUser?id="+id)
        if(res=="success"){
          this.$message.success("删除成功")
        }
      }
        this.getUserList();
    },
    handleCloses(){
      this.$refs.ruleForms.resetFields();
    },
   
   async showEditDialog(id){
   const {data:res} = await this.$http.get("/getUser?id="+id)
   
   this.ruleForms=res;
   this.updateDialogVisible=true
   },
   async updateUser(){
      const {data:res} = await this.$http.put("/updateUser",this.ruleForms)
      
      this.updateDialogVisible=false;
      this.getUserList();
    },
     
  }
};
</script>
<style  scoped>
.el-breadcrumb {
  margin-bottom: 15px;
  font-size: 17px;
}
</style>